home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / Xprof / xmeasure / runtests.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  12KB  |  432 lines

  1. /*==================================================================
  2.  *      File :          runtests.c
  3.  *    Package:    Xmeasure
  4.  * 
  5.  *      Author :        Aloke Gupta.
  6.  *
  7.  *  (C) Copyright 1992, Aloke Gupta.
  8.  *  All rights granted to University of Illinois Board of Regents.
  9.  *==================================================================*/
  10. /*
  11.  * int RunTests (XD *xd)
  12.  * int Calibrate(XD *xd, TestRequest *test, GC gc, SizePair *sizepair)
  13.  * InitGCarray (XD *xd)
  14.  * PrintGfxvals(FILE *fp, int gxmode, int linestyle, int fillstyle,
  15.  *            int linewidth)
  16.  */
  17.  
  18. #include "perf.h"
  19.  
  20. int debuglevel=1;
  21. /*
  22. int debuglevel = 0; 
  23. int debuglevel = 2;
  24. */
  25.  
  26. static float testtime       = 1.0; /* Each test is run for 1.0 seconds    */
  27. static float calibratetime = 1.0; /* Run calibration steps for 1.0 secs  */
  28. static int   repeats       = 3  ; /* Number of times to repeat each test */
  29. static int   calibrate_reps= 6;
  30.  
  31. extern TestRequest testrequest[];
  32. extern char     *FontName[];
  33. extern unsigned NUMFONTS;
  34.  
  35. SetTesttime(time)
  36. float time;
  37. {   testtime = time; }
  38.  
  39. SetRepeats(reps)
  40. int   reps;
  41.     int tmp;
  42.     repeats  = reps;
  43.     tmp = 2 * reps;
  44.     if (tmp > calibrate_reps)
  45.     calibrate_reps = 6;
  46. }
  47.  
  48. RunTests(xd)
  49. XD *xd;
  50. {
  51.     int        i, j, count;
  52.     double    *timespent;    /* Array of dimension "repeats" */
  53.     TestRequest *testptr;
  54.     char    message[128];
  55.     XParams    xp;        /* Parameters to pass to computations */
  56.  
  57.     /*
  58.      *  Allocate the "timespent" array
  59.      */
  60.     timespent = (double *) calloc((unsigned) repeats, sizeof(double));
  61.     /*
  62.      *  Main loop.
  63.      */
  64.     for (i = 0; i < MAXREQUESTS; i++) {    /* For each request */
  65.     testptr = &testrequest[i];    /* Init. testptr for this request */
  66.     if (testptr->dothis != TRUE)
  67.         continue;            /* Skip this iteration */
  68.     XDestroySubwindows(xd->display, xd->window);
  69.     XClearWindow(xd->display, xd->window);    /* Clear the display surface */
  70.     /*
  71.      * Do the computation for each size pair of this request
  72.      */
  73.     for (j=0; j< MAXSIZEPAIRS; j++){/* For each size pair of this request */
  74.       if (testptr->sizepair[j].s1 == INVALID)
  75.         break;        /* First invalid pair causes termination */
  76.       xp.sizepair = &testptr->sizepair[j];
  77.       /* The following are set in the init function from the knowledge of
  78.          the size pair, and the number of objects to process in one call. */
  79.       xp.size = 0;        /* default value of size is zero */
  80.       xp.numobjs = 1;    /* default number of objects to process in
  81.                    one call is one */
  82.       switch(testptr->msgclass) {
  83.         case WIN:
  84.         xp.gc = xd->gc;
  85.         Calibrate(xd, testptr, &xp);    /* Set xp.itns */
  86.         if (xp.itns == 0)    /* Error flag */
  87.             break;
  88.         printf("# %s ", testptr->name);
  89.         if (debuglevel > 0) fflush(stdout);
  90.         for (count = 0; count < repeats; count++) {
  91.             sprintf(message,
  92.             "Testing Xlib %s function: %s. Test %d of %d", 
  93.              ( testptr->msgclass == WIN) ? "windowing":
  94.              ((testptr->msgclass == TXT) ? "text"  : "graphics"),
  95.              testptr->name, count+1, repeats);
  96.             printmessage(xd, message);
  97.             testptr->init(xd, &xp);
  98.             Synchronize(xd);
  99.             InitTimer();
  100.             testptr->func(xd, &xp);
  101.             Synchronize(xd);
  102.             timespent[count] = TimeSpent();
  103.             testptr->cleanup(xd, &xp);
  104.             printf("(%d,%6.3lf) ",xp.size,
  105.             (double) (xp.itns * xp.numobjs) /timespent[count]);
  106.             if (debuglevel > 0) fflush(stdout);
  107.         }
  108.         printf("\n%s   (%d,%6.3lf) ", testptr->name, xp.size,
  109.             (double) (xp.itns * xp.numobjs)/median(timespent,repeats));
  110.         printf("\n");
  111.         if (debuglevel > 0) fflush(stdout);
  112.         break;
  113.         case GFX: {
  114.         int gxmode=0, linestyle=0, fillstyle=0, linewidth=0;
  115.         int gfxslot;
  116.         /*
  117.          * Now run the tests for each of the graphics type functions
  118.          */
  119.         for (linewidth=0; linewidth < NUMLINEWIDTHS; linewidth++)
  120.          for (fillstyle=0; fillstyle < NUMFILLSTYLES; fillstyle++)
  121.           for (linestyle=0; linestyle < NUMLINESTYLES; linestyle++)
  122.            for (gxmode =0; gxmode < NUMGXMODES; gxmode++)
  123.  
  124.            {
  125.             gfxslot = GFXINDEX(gxmode, linestyle, fillstyle, linewidth);
  126.             if (debuglevel > 1) {
  127.             printf("\ngfxslot = %d\n", gfxslot);
  128.             }
  129.             if (debuglevel > 0) fflush(stdout);
  130.             XClearWindow(xd->display, xd->window);
  131.             xp.gc = xd->gcarray[gfxslot];
  132.             Calibrate(xd, testptr, &xp);
  133.             if (xp.itns == 0)    /* Error flag */
  134.                 break;
  135.             printf("# %s ", testptr->name);
  136.             for (count = 0; count < repeats; count++) {
  137.             sprintf(message,
  138.                 "Testing Xlib %s function: %s. Test %d of %d",
  139.                 ( testptr->msgclass == WIN) ? "windowing":
  140.                 ((testptr->msgclass == TXT) ? "text"  : "graphics"),
  141.                 testptr->name, count+1, repeats);
  142.             printmessage(xd, message);
  143.             XClearWindow(xd->display, xd->window);
  144.                 /* Initialize done outside these two loops */
  145.             testptr->init(xd, &xp);    
  146.             Synchronize(xd);
  147.             InitTimer();
  148.             testptr->func   (xd, &xp);
  149.             Synchronize(xd);
  150.             testptr->cleanup(xd, &xp);
  151.             timespent[count] = TimeSpent();
  152.                 /* Cleanup done outside these two loops */
  153.             printf("(%d, %6.3lf) ",xp.size,
  154.                 (double) (xp.itns * xp.numobjs) /timespent[count]);
  155.             if (debuglevel > 0) fflush(stdout);
  156.             }
  157.             printf("\n");
  158.             printf("%-20s ", testptr->name);
  159.             PrintGfxvals(stdout, gxmode, linestyle, fillstyle,
  160.                         linewidth);
  161.             printf(" (%d,%6.3lf) ", xp.size,
  162.             (double)(xp.itns*xp.numobjs)/median(timespent,repeats));
  163.             printf("\n");
  164.             if (debuglevel > 0) fflush(stdout);
  165.          }
  166.         break;
  167.         }
  168.         case TXT: {
  169.         int txtslot=0;
  170.         /*
  171.          * Now run the tests for each of the fonts 
  172.          */
  173.         for (txtslot=0; txtslot < NUMFONTS; txtslot++) {
  174.            if (FontName[txtslot] == NULL)
  175.             break;
  176.             if (debuglevel > 1) {
  177.             printf("\ntxtslot = %d\n", txtslot);
  178.             }
  179.             if (debuglevel > 0) fflush(stdout);
  180.             XClearWindow(xd->display, xd->window);
  181.             xp.sizepair->s2=txtslot;    /* Which font is this ? */
  182.  
  183.             xp.gc = xd->gc;
  184.  
  185.             Calibrate(xd, testptr, &xp);
  186.             if (xp.itns == 0)    /* Error flag */
  187.                 break;
  188.             printf("# %s ", testptr->name);
  189.             for (count = 0; count < repeats; count++) {
  190.             sprintf(message,
  191.                 "Testing Xlib %s function: %s. Test %d of %d",
  192.                 ( testptr->msgclass == WIN) ? "windowing":
  193.                 ((testptr->msgclass == TXT) ? "text"  : "graphics"),
  194.                 testptr->name, count+1, repeats);
  195.             printmessage(xd, message);
  196.             XClearWindow(xd->display, xd->window);
  197.                 /* Initialize done outside these two loops */
  198.             testptr->init(xd, &xp);    
  199.             Synchronize(xd);
  200.             InitTimer();
  201.             testptr->func   (xd, &xp);
  202.             Synchronize(xd);
  203.             testptr->cleanup(xd, &xp);
  204.             timespent[count] = TimeSpent();
  205.                 /* Cleanup done outside these two loops */
  206.             printf("(%d, %6.3lf) ",xp.size,
  207.                 (double) (xp.itns * xp.numobjs) /timespent[count]);
  208.             if (debuglevel > 0) fflush(stdout);
  209.             }
  210.             printf("\n");
  211.             printf("%-12s ", testptr->name);
  212.             printf("fontname=%s", FontName[txtslot]);
  213.             printf(" (%d,%6.3lf) ", xp.size,
  214.             (double)(xp.itns*xp.numobjs)/median(timespent,repeats));
  215.             printf("\n");
  216.             if (debuglevel > 0) fflush(stdout);
  217.          }
  218.         break;
  219.         }
  220.         default:
  221.         break;
  222.       }    /* switch(testptr->msgclass) */
  223.     }    /* for (j=0; j < MAXSIZEPAIRS ... ) */
  224.     }        /* for (i=0; i < MAXREQUESTS  ... ) */
  225. }
  226.  
  227. #define MAXSCALE 10.0
  228. #define MINSCALE (1.0 / MAXSCALE) 
  229. Calibrate(xd, test, xp)
  230. XD        *xd;
  231. TestRequest *test;
  232. XParams        *xp;
  233. {
  234.     double timespent;
  235.     double scale = 0.0;
  236.     long   low=1,    high=MAXLONG;
  237.     double tlow=0.0, thigh=MAXDOUBLE;
  238.     long   saveitns;
  239.     int    i;
  240.     char   message[128];
  241.  
  242.     sprintf(message, "Calibrating Xlib %s function: %s.", 
  243.     ( test->msgclass == WIN) ? "windowing":
  244.     ((test->msgclass == TXT) ? "text"     : "graphics"),
  245.     test->name);
  246.     printmessage(xd, message);
  247.  
  248.     xp->itns = 1;    /* Initially run only one test */
  249.     if (debuglevel > 1)
  250.     printf("Calibrating Request %d: %s\n", test->number, test->name);
  251.     for (i =0; i < calibrate_reps; i++) {/* Estimate no. of iterations needed */
  252.     if (debuglevel > 1) {
  253.         printf("high = %10ld, low = %5ld ",high, low);
  254.         printf("itns = %5ld ", xp->itns); fflush(stdout);
  255.     }
  256.     test->init(xd, xp);
  257. if (xp->itns == 0) {/* An error has occured */
  258.     printf("# Calibrate: Cannot calibrate %s\n", test->name);
  259.     test->cleanup(xd, xp);
  260.     return;
  261. }
  262.     Synchronize(xd);
  263.     InitTimer();
  264.     test->func(xd, xp);
  265.     Synchronize(xd);
  266.     timespent = TimeSpent();
  267.     test->cleanup(xd, xp);
  268.  
  269.     if (timespent < .01)            /* Clock grain 10 ms */
  270.         timespent = .01;
  271.     if (debuglevel > 1){
  272.         printf("timespent = %6.4lf\n", timespent);
  273.         fflush(stdout);
  274.     }
  275.     saveitns = xp->itns;
  276.     scale = calibratetime / timespent;    /* How much to scale ? */
  277.     if ((scale > 0.9 ) && (scale < 2.0))
  278.         break;        /* These many iterations are enough */
  279.  
  280.     if      (scale > MAXSCALE) scale = MAXSCALE;
  281.     else if (scale < MINSCALE) scale = MINSCALE;
  282.  
  283.     /* Now compute the new figure of itns */
  284.     /*
  285.     xp->itns = (long) (xp->itns * scale);
  286.     */
  287.  
  288.     /* Set new high and low watermarks */
  289.     if (scale < 1.0) {    /* We overshot */
  290.         if (high > saveitns) {
  291.         high = saveitns;
  292.         thigh = timespent;
  293.         }
  294.     }
  295.     else  {            /* We undershot */
  296.         if (low  < saveitns) {
  297.         low = saveitns;
  298.         tlow = timespent;
  299.         }
  300.     }
  301.     /* Now compute the new figure for iterations */
  302.     if (high != MAXLONG) {    /* Don't bother if it is still initial value */
  303.         if (xp->itns > low)
  304.         xp->itns = (long) (low + ((high - low) * (calibratetime - tlow) /
  305.                          (thigh         - tlow) ));
  306.     }
  307.     else xp->itns = (long) (xp->itns * scale);
  308.  
  309.         if (xp->itns == 0)
  310.         xp->itns = 1;
  311.     if (xp->itns == 1)
  312.         break;
  313.     }
  314.  
  315.     /* Normalize the number of iterations needed to go upto "testtime" */
  316.     scale = testtime / timespent;
  317.     xp->itns = (long) (xp->itns * scale);
  318.     if (xp->itns == 0) 
  319.         xp->itns = 1;
  320.  
  321. if (debuglevel > 1) {
  322.     printf("Final itns returned by Calibrate = %4ld\n", xp->itns);
  323.     fflush(stdout);
  324. }
  325. }
  326.  
  327. int LineWidths[] = { 0, 1, 3, 10};
  328.  
  329. /*
  330.  * Initialize the 32 graphics contexts to be used for graphics operations
  331.  */
  332. InitGCarray(xd)
  333. XD *xd;
  334. {
  335.     XGCValues values;
  336.     unsigned long valuemask=0;
  337.     int gxmode, linestyle, fillstyle, linewidth;
  338.     int gfxslot;
  339.  
  340.     valuemask = GCFunction   | GCLineStyle | GCFillStyle | GCLineWidth |
  341.         GCForeground | GCBackground;
  342.     values.foreground = xd->foreground;
  343.     values.background = xd->background;
  344.  
  345.     for (   linewidth = 0; linewidth < NUMLINEWIDTHS; linewidth++)
  346.      for (  fillstyle = 0; fillstyle < NUMFILLSTYLES; fillstyle++)
  347.       for ( linestyle = 0; linestyle < NUMLINESTYLES; linestyle++)
  348.        for (gxmode    = 0; gxmode    < NUMGXMODES   ; gxmode   ++)
  349.        {
  350.         /*
  351.          * Set the mode for the Graphics function
  352.          */
  353.         switch(gxmode) {    /* Can have two values */
  354.         case 0:  values.function = GXcopy;
  355.              break;
  356.         default: values.function = GXxor;
  357.              break;
  358.         }
  359.         /* 
  360.          * Set the linestyle
  361.          */
  362.         switch(linestyle) {    /* Can have two values */
  363.         case 0:  values.line_style = LineSolid;
  364.              break;
  365.         default: values.line_style = LineDoubleDash;
  366.              break;
  367.         }
  368.         /*
  369.          * Set the fillstyle
  370.          */
  371.         switch(fillstyle) {    /* Can have two values */
  372.         case 0:  values.fill_style = FillSolid;
  373.              break;
  374.         default: values.fill_style = FillOpaqueStippled;
  375.              break;
  376.         }
  377.         /*
  378.          * Set line width
  379.          */
  380.         values.line_width = LineWidths[linewidth];
  381.  
  382.         /*
  383.          * Now initialize the graphics context
  384.          */
  385.         gfxslot = GFXINDEX(gxmode, linestyle, fillstyle, linewidth);
  386.         xd->gcarray[gfxslot] = XCreateGC(xd->display, xd->window,
  387.                      valuemask, &values);
  388.        }
  389. }
  390.  
  391. #define BUFSIZE 128
  392. PrintGfxvals(fp, gxmode, linestyle, fillstyle, linewidth)
  393. FILE *fp;
  394. int gxmode, linestyle, fillstyle, linewidth;
  395. {
  396.     char buffer[BUFSIZE], tmp[BUFSIZE];
  397.  
  398.     bzero (buffer, BUFSIZE);
  399.     /* Print the mode for the Graphics function */
  400.     switch(gxmode) {    /* Can have two values */
  401.     case 0: 
  402.         strcat(buffer, "gxmode=GXcopy ");
  403.         break;
  404.     default:
  405.         strcat(buffer, "gxmode=GXxor  ");
  406.         break;
  407.     }
  408.     /* Print the linestyle */
  409.     switch(linestyle) {    /* Can have two values */
  410.     case 0: 
  411.         strcat(buffer, "linestyle=LineSolid      ");
  412.         break;
  413.     default:
  414.         strcat(buffer, "linestyle=LineDoubleDash ");
  415.         break;
  416.     }
  417.     /* Print the fillstyle */
  418.     switch(fillstyle) {    /* Can have two values */
  419.     case 0: 
  420.         strcat(buffer, "fillstyle=FillSolid          ");
  421.         break;
  422.     default:
  423.         strcat(buffer, "fillstyle=FillOpaqueStippled ");
  424.         break;
  425.     }
  426.     /* Print line width */
  427.     sprintf(tmp, "linewidth=%-2d", LineWidths[linewidth] );
  428.     strcat (buffer, tmp);
  429.     fprintf(fp, "%s", buffer);
  430. }
  431.